//
//  TextCell.swift
//  CellAutolayoutHeight
//
//  Created by zk_01 on 2022/3/29.
//

import UIKit

class TextCell: BaseCell {
    
    let contentLab_rightMargin = 10.0
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.contentLab.snp.makeConstraints { make in
            make.top.equalTo(self.portraitImgView.snp.bottom).offset(10)
            make.left.equalTo(self.nameLab)
            make.right.equalToSuperview().offset(-self.contentLab_rightMargin)
        }
        
        self.hotTopicsStackView.snp.makeConstraints { make in
            make.top.equalTo(self.contentLab.snp.bottom).offset(10)
            make.left.equalToSuperview().offset(30)
            make.right.equalToSuperview().offset(-10)
            make.bottom.equalTo(self.bottomBarStackView.snp.top).offset(-10)
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func setModel(_ model: DataModel) {
        super.setModel(model)
        self.contentLab.numberOfLines = model.textLineCount
        self.contentLab.text = model.content
        if model.textLineCount == 9 {
            self.setTruncationToken(self.contentLab)
        }else{
            self.updateTextLabel(self.contentLab)
        }
        self.hotTopicsStackView.subviews.forEach { v in
            v.removeFromSuperview()
        }
        model.hotTopics.forEach { hot in
            let lab = UILabel()
            lab.text = "#" + hot.title
            lab.textColor = .systemBlue
            lab.textAlignment = .left
            lab.font = .systemFont(ofSize: 16.0)
            self.hotTopicsStackView.addArrangedSubview(lab)
        }
    }
    
    func setTruncationToken(_ tagerLab:YYLabel){
        let expandString = NSMutableAttributedString.init(string: "...展开")
        expandString.yy_font = tagerLab.font
        expandString.yy_setColor(UIColor(red: 0.0, green: 0.449, blue: 1.0, alpha: 1.0), range: NSMakeRange(3, 2))

        let highlight = YYTextHighlight()
        highlight.setFont(tagerLab.font)
        highlight.setColor(UIColor(red: 0.578, green: 0.790, blue: 1.0, alpha: 1.0))
        highlight.tapAction = { [weak self] (containerView, text, range, rect) in
            guard let self = self,let model = self.model,let lab = containerView as? YYLabel else { return  }
            self.model?.textLineCount = 0
            self.updateTextLabel(lab)
            self.events?(self, model, .contentCollapse(false))
        }
//        NSMakeRange(3, 2)
        expandString.yy_setTextHighlight(highlight, range: expandString.yy_rangeOfAll())

        let expandLabel = YYLabel()
        expandLabel.attributedText = expandString
        expandLabel.sizeToFit()

        let truncationToken = NSMutableAttributedString.yy_attachmentString(withContent: expandLabel, contentMode: .center, attachmentSize: expandLabel.size, alignTo: tagerLab.font, alignment: .center)
        tagerLab.truncationToken = truncationToken
    }
    
    func updateTextLabel(_ tagerLab:YYLabel){
        let collapseString = NSMutableAttributedString.init(string: " 收起")
        collapseString.yy_font = tagerLab.font
        collapseString.yy_color = UIColor(red: 0.0, green: 0.449, blue: 1.0, alpha: 1.0)

        let highlight = YYTextHighlight()
        highlight.setFont(tagerLab.font)
        highlight.setColor(UIColor(red: 0.578, green: 0.790, blue: 1.0, alpha: 1.0))
        highlight.tapAction = { [weak self] (containerView, text, range, rect) in
            guard let self = self,let model = self.model else { return  }
            self.model!.textLineCount = 9
            self.events?(self, model, .contentCollapse(true))
        }

        collapseString.yy_setTextHighlight(highlight, range: collapseString.yy_rangeOfAll())

        let attributedString = NSMutableAttributedString.init(string: tagerLab.text ?? "", attributes: [.font:tagerLab.font ?? UIFont.systemFont(ofSize: 16.0),.foregroundColor: tagerLab.textColor ?? .black])
        attributedString.yy_setTextHighlight(highlight, range: NSMakeRange(attributedString.length - 2, 2))
        attributedString.append(collapseString)

        tagerLab.attributedText = attributedString
        tagerLab.sizeToFit()
    }
    
    lazy var contentLab: YYLabel = {
        let lab = YYLabel()
        lab.textColor = .gray
        lab.textAlignment = .left
        lab.font = .systemFont(ofSize: 14.0)
        lab.preferredMaxLayoutWidth = UIScreen.main.bounds.width - self.nameLab_LeftMargin - self.portraitImgView_W - self.portraitImgView_Margin - self.contentLab_rightMargin;
        lab.textVerticalAlignment = .top
        self.contentView.addSubview(lab)
        return lab
    }()
}
